package marble.model {

import flash.display.Bitmap;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.navigateToURL;

import marble.Facade;

import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;

public class PlanInfoButton extends SimpleButton {

		private var elem:Element;

		[Embed(source="/assets/lightbulb_dis.png")]
		private var PlanDetailsDis:Class;

		[Embed(source="/assets/lightbulb.png")]
		private var PlanDetails:Class;

		private var info:InfoPopup;

		private var facade:Facade = Facade.getInstance();

		public function PlanInfoButton(elem:Element) {

			var img:Bitmap = new PlanDetails();
			var img_dis:Bitmap = new PlanDetailsDis();

			super(img_dis, img, img, img);
			this.elem = elem;
			
			addEventListener(MouseEvent.ROLL_OVER, onMouseOver);
			addEventListener(MouseEvent.ROLL_OUT, onMouseLeave);
			addEventListener(MouseEvent.CLICK, onMouseClick);
		}

		private function onMouseOver(event:MouseEvent):void {
			if (info == null) {
				
				info = InfoPopup(PopUpManager.createPopUp(this, InfoPopup, false));
				info.elem = elem;
				var a:int = (parent.mouseX > parent.width/2) ? 1 : 0;
				info.x = x + 60 - a*(width + info.width + 10);
				info.y = y + 30;
			} else {
				PopUpManager.addPopUp(info, this);
			}
		}

		private function onMouseLeave(event:MouseEvent):void {
			if (info) {
				PopUpManager.removePopUp(info);
			}
		}
		
		private function onMouseClick(event:MouseEvent):void {
			facade.callRemote("getShowConferenceElementUrl", [elem.id],
				function (event:ResultEvent):void {
					var url:String = event.result.toString().replace("&amp;", "&");
					navigateToURL(new URLRequest(url), "_self");
				}
			);
		}
	}
}